package com.chinasofti.shoot;

import java.awt.image.BufferedImage;

//英雄机 是飞行物
public class Hero extends FlyingObject {


    private int life;
    private int doubleFire;//火力值
    private BufferedImage[] images = {};//图片数组
    private int index;//辅助图片切换

    //构造方法


    public Hero() {
        image = ShootGame.hero0;
        width = image.getWidth();
        height = image.getHeight();
        x = 150; //固定的x坐标
        y = 400;//固定的y坐标
        life = 3;
        doubleFire = 0;
        images = new BufferedImage[]{ShootGame.hero0,ShootGame.hero1};//两张图片
        index = 0;
    }

    @Override
    public void step() {//100毫秒切换
        image = images[index++/10%images.length];
    }

    @Override
    public boolean outOfBounds() {
        return false;//越远不越界
    }

    //英雄机发射子弹
    public Bullet[] shoot(){
        int xStep = this.width/4;
        if(doubleFire > 0){ //双倍火力
            Bullet[] bullets = new Bullet[3];
            bullets[0] = new Bullet(this.x+1*xStep,this.y-20);
            bullets[1] = new Bullet(this.x+2*xStep,this.y-20);
            bullets[2] = new Bullet(this.x+3*xStep,this.y-20);
            doubleFire -= 2;
            return bullets;
        }else {
            Bullet[] bullets = new Bullet[2];
            bullets[0] = new Bullet(this.x+1*xStep,this.y-20);
            bullets[1] = new Bullet(this.x+3*xStep,this.y-20);
            return bullets;
        }
    }
    //让英雄机随着鼠标移动
    public void moveTo(int x,int y){
        this.x = x - this.width/2;
        this.y = y - this.height/2;
    }
    //加活力值
    public void addDoubleFire(){
        doubleFire += 40;
    }
    //增加生命
    public void addLife() {
        life++;
    }
    //获取生命
    public int getLife(){
        return life;
    }
    //判断英雄机与敌人的碰撞
    public boolean hit(FlyingObject obj){
        int x1 = obj.x - this.width/2;
        int x2 = obj.x + obj.width + this.width/2;
        int y1 = obj.y - this.height/2;
        int y2 = obj.y + obj.height + this.height/2;
        int x = this.x + this.width/2;
        int y = this.y + this.height/2;

        return x > x1 && x < x2
                &&
                y > y1 && y < y2;
    }
    //减少生命
    public void subtractLife(){
        life--;
    }
    //火力清零
    public void setDoubleFire(int doubleFire){
        this.doubleFire = doubleFire;
    }

}
